﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using SF.Common;

namespace SF.Ucenter.admin
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                txtUserName.Text = Utils.GetCookie("DTRememberName");

                //Test
                txtUserName.Text = "labix";
                //txtPassword.Text = "12345679";
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string userName = txtUserName.Text.Trim();
            string userPwd = txtPassword.Text.Trim();

            if (userName.Equals("") || userPwd.Equals(""))
            {
                msgtip.InnerHtml = "请输入用户名或密码";
                return;
            }
            if (Session["AdminLoginSun"] == null)
            {
                Session["AdminLoginSun"] = 1;
            }
            else
            {
                Session["AdminLoginSun"] = Convert.ToInt32(Session["AdminLoginSun"]) + 1;
            }
            //判断登录错误次数
            if (Session["AdminLoginSun"] != null && Convert.ToInt32(Session["AdminLoginSun"]) > 5)
            {
                msgtip.InnerHtml = "错误超过5次，关闭浏览器重新登录！";
                return;
            }
            BLL.manager bll = new BLL.manager();
            Model.manager model = bll.GetModel(userName, userPwd, true);
            if (model == null)
            {
                msgtip.InnerHtml = "用户名或密码有误，请重试！";
                return;
            }
            //写入登录日志
            Model.siteconfig siteConfig = new BLL.siteconfig().loadConfig();
            if (siteConfig.logstatus > 0)
            {
                new BLL.manager_log().Add(model.id, model.user_name, DTEnums.ActionEnum.Login.ToString(), "用户登录");
            }
            Session.Timeout = 45;
            Session[DTKeys.SESSION_ADMIN_INFO] = model;
            if (model.type == 0)
            {
                //写入Cookies
                Utils.WriteCookie("DTRememberName", model.user_name, 14400);
                Utils.WriteCookie("AdminName", "DTcms", model.user_name);
                Utils.WriteCookie("AdminPwd", "DTcms", model.password);
                Response.Redirect("index.aspx");
            }
            else if (model.type == 1)
            {
                //写入Cookies
                Utils.WriteCookie("BusnieseRememberName", model.user_name, 14400);
                Utils.WriteCookie("BusnieseName", "Busniese", model.user_name);
                Utils.WriteCookie("BusniesePwd", "Busniese", model.password);
                //Session[DTKeys.SESSION_APP_INFO] = "wx0b7e3ae9164b0fd9";
                //Session[DTKeys.SESSION_TEMPLATES_INFO] = 1;
                //Session[DTKeys.SESSION_BUSNIESE_NUM] = model.busnieseNum;
                Response.Redirect("business/frame.aspx");
                //Response.Redirect("test.aspx?test=" + model.busnieseNum);
            }
            return;
        }

    }
}